﻿using Sustainalytics.ClientDW.Synchronization.RiskRatingsBenchmarks;
using Sustainalytics.Entities.RiskRatings;
using System.Linq;
using Xunit;

namespace Sustainalytics.Benchmarkings.Tests
{


    [Trait("Category", "Unit")]
    public class HistogramTest : IClassFixture<ComparativeGroupFixture>
    {
        private readonly ComparativeGroupFixture _fixture;
        private readonly Histogram<RiskRatingResearchProfile, int> _sut;

        public HistogramTest(ComparativeGroupFixture fixture)
        {
            _fixture = fixture;
            _sut = Histogram<RiskRatingResearchProfile, int>.Create(_fixture.GetCorporateData(), x => x.SubPeerGroupId, x => x.ManagementRisk.UnmanagedRisk.Value, new DoubleComparer());

        }

        [Fact]
        public void AllHistogram_Values_Sum_should_equal_Companies_Count()
        {
          

       
            var histograms = _sut.GetHistograms();


            var actual = histograms.Sum(s => s.Item2.Sum());

            var expected = _fixture.GetCorporateData().Count;

            Assert.Equal(expected, actual);
        }


        [Fact]
        public void Histogram_Value_Count_should_equal_group_count()
        {


            var histograms = _sut.GetHistograms();

            var companies = _fixture.GetCorporateData();

            var peerGroups = _fixture.GetSubPeerGroups();

            Assert.All(histograms, hist =>
            {

                var expected = companies.Count(x => x.SubPeerGroupId == hist.Item1);
                Assert.Equal(expected, hist.Item2.Sum());
            });

        }


        [Fact]
        public void Histogram_Count_should_equal_group_count()
        {


            var histograms = _sut.GetHistograms();

            var companies = _fixture.GetCorporateData();

            var peerGroups = _fixture.GetSubPeerGroups();

            Assert.All(histograms, hist =>
            {

                var expected = companies.Count(x => x.SubPeerGroupId == hist.Item1);
                Assert.Equal(expected, hist.Item3);
            });

        }
    }
}